package com.supor.aiot.mine.ui.setting;

import android.content.Intent;
import android.view.View;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.SystemUtils;
import com.android.baseconfig.common.utils.ToastUtils;
import com.android.baseconfig.common.views.AppBasicDialog;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.SuporApplication;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.helper.WsHelper;
import com.supor.aiot.common.push.PushMsgService;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.databinding.ActivitySettingBinding;

import io.reactivex.rxjava3.functions.Consumer;

public class SettingActivity extends BaseMVVMActivity<SettingViewModel, ActivitySettingBinding> implements View.OnClickListener {

    private AppBasicDialog deleteDialog;

    @Override
    protected int resourceId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.setViewM(this);
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());

        bindingView.tvVersion.setText(SystemUtils.getVerName(SuporApplication.getInstance()));
        bindingView.rlPrivacySet.setOnClickListener(this);
        bindingView.rlPrivacyService.setOnClickListener(this);
        bindingView.rlVersionUpdate.setOnClickListener(this);
        bindingView.rlAbout.setOnClickListener(this);
        bindingView.rlSystemPermission.setOnClickListener(this);
        
        bindingView.cvLogout.setOnClickListener(v ->
                showLogoutDialog(getString(R.string.delete_user_title), getString(R.string.logout_confirm)));

        if (!AppAgent.isLogin()) bindingView.llBtn.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel.getLogoutResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                ToastUtils.getInstance().showCustomToast("退出登录成功");
                PushMsgService.unbindUser();
                BaseSharedPreferences.setString(AppAgent.getApp(), Constants.Key.USER_INFO, "");
                BaseSharedPreferences.setString(this, HttpConstant.TOKEN, "");
                GlobalDataCache.userInfo = null;
                GlobalDataCache.deviceTree = null;
                AppAgent.setLogin(false);
                WsHelper.getInstance().exit();
                RxBus3.getInstance().post(Constants.Tag.LOGOUT, Constants.Value.SUCCESS);

                bindingView.llBtn.setVisibility(View.GONE);
                finish();
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("退出登录失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        RxBus3.getInstance().register(Constants.Tag.LOGOUT, (Consumer<String>) s -> {
            if (Constants.Value.SUCCESS.equalsIgnoreCase(s)) {
                if (bindingView.llBtn.getVisibility() != View.GONE) {
                    bindingView.llBtn.setVisibility(View.GONE);
                }
            }
        }, this.getClass());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_privacy_set) {
            startActivity(new Intent(this, PrivacySetActivity.class));
        } else if (id == R.id.rl_privacy_service) {
            startActivity(new Intent(this, PrivacySafeServiceActivity.class));
        } else if (id == R.id.rl_version_update) {
            startActivity(new Intent(this, VersionUpdateActivity.class));
        } else if (id == R.id.rl_about) {
            startActivity(new Intent(this, AboutActivity.class));
        } else if (id == R.id.rl_system_permission) {
            startActivity(new Intent(this, SystemPermissionActivity.class));
        }
    }

    private void showLogoutDialog(String title, String message) {
        if (deleteDialog != null && deleteDialog.isShowing()) {
            deleteDialog.dismiss();
        }
        AppBasicDialog.Builder builder = new AppBasicDialog.Builder(this, false);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.sure, (dialog, which) -> {
            showLoading();
            viewModel.logout();
            dialog.cancel();
        });
        builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel());
        deleteDialog = builder.create();
        deleteDialog.setCancelable(false);
        deleteDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus3.getInstance().unregister(this.getClass());
    }
}